package com.aishua.fire.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.constant.DictConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.jpa.entity.Dictionary;
import com.aishua.fire.mapper.DictionaryMapper;
import com.aishua.fire.mapper.dto.dictionary.DictionaryDictListDto;
import com.aishua.fire.mapper.dto.dictionary.DictionaryDto;
import com.aishua.fire.mapper.dto.dictionary.DictionaryListDto;
import com.aishua.fire.service.DictionaryService;

/**
 * 字典管理service层类
 * @author Administrator
 *
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	@Autowired
	private ValidateUtil validateUtil;
	
	//查询字典表的id和name(字典帮助)
	public List<DictionaryDto> findIdAndName(String typeCode) {
		List<DictionaryDto> idAndName = dictionaryMapper.findIdAndName(typeCode);
		return idAndName;
	}
		
	//查出大类list
	public List<DictionaryDictListDto> findDicList() {
		List<DictionaryDictListDto> dicList = dictionaryMapper.findDicList();
		return dicList;
	}

	//查出小类list
	public List<DictionaryListDto> findDictionaryList(String typeCode) {
		List<DictionaryListDto> dictionaryList = dictionaryMapper.findDictionaryList(typeCode);
		return dictionaryList;
	}

	//大类添加
	@Transactional
	public void addTypeCode(String typeCode,String typeValue) throws ValidateException {
		//添加之前,先进行重复性验证(typeCode)
		Long isRepeat = dictionaryMapper.isRepeatDict(typeCode,typeValue);
		if (isRepeat==0) {//不重复
			Dictionary dictionary=new Dictionary();
			dictionary.setDelStatus(DictConstants.DELETE_NO);
			dictionary.setTypeCode(typeCode);
			dictionary.setTypeValue(typeValue);
			dictionaryMapper.addTypeCode(dictionary);
		}else {//重复
			validateUtil.addCustomerError("字典类别重复");
		}
		
		
	}

	//大类修改
	@Transactional
	public void updateTypeCode(String typeCode, String typeValue) throws ValidateException {
		//修改之前,先进行重复性验证(typeValue)
		Long isRepeat = dictionaryMapper.isRepeatTypeValue(typeCode, typeValue);
		if (isRepeat==0) {//不重复
			Dictionary dictionary=new Dictionary();
			dictionary.setTypeCode(typeCode);
			dictionary.setTypeValue(typeValue);
			dictionaryMapper.updateTypeCode(dictionary);
		}else {//重复
			validateUtil.addCustomerError("字典类别重复");
		}
	}

	//大类删除
	@Transactional
	public void deleteTypeCode(String typeCode) {
		dictionaryMapper.deleteTypeCode(typeCode);
	}

	//小类添加
	@Transactional
	public void addDictName(String typeCode, String typeValue, String dictName) throws ValidateException {
		//执行新增操作,先进行重复性验证
		Long isRepeat = dictionaryMapper.isRepeatDictNameAdd(typeCode, dictName);
		if (isRepeat==0) {//不重复
			Dictionary dictionary=new Dictionary();
			dictionary.setDelStatus(DictConstants.DELETE_NO);
			dictionary.setTypeCode(typeCode);
			dictionary.setTypeValue(typeValue);
			dictionary.setDictName(dictName);
			dictionaryMapper.addDictName(dictionary);
		}else {//重复
			validateUtil.addCustomerError("数值名称重复");
		}
		
	}

	//小类修改
	@Transactional
	public void updateDictName(String typeCode,String dictName,Integer id) throws ValidateException {
		//执行修改操作,先进行重复性验证
		Long isRepeat = dictionaryMapper.isRepeatDictNameUpdate(typeCode, dictName, id);
		if (isRepeat==0) {//不重复
			Dictionary dictionary=new Dictionary();
			dictionary.setId(id);
			dictionary.setDictName(dictName);
			dictionaryMapper.updateDictName(dictionary);
		}else {//重复
			validateUtil.addCustomerError("数值名称重复");
		}
	}
		

	//小类删除
	@Transactional
	public void deleteDictName(Integer id) {
		dictionaryMapper.deleteDictName(id);
	}

	
	
}
